package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMoreCarriageInfoBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.CarrierDetailsEntity;
import com.sgb.kjwl.view.ui.adapter.transport.CarriageInfoAdapter;
import com.sgb.kjwl.viewmodel.transport.MoreCarriageInfoModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * Time:2021/1/12
 * Author:黄淋
 * Description:更多承运信息
 */
public class MoreCarriageInfoActivity extends AppActivity {
    private ActivityMoreCarriageInfoBinding mBinding;
    @Inject
    MoreCarriageInfoModel mModel;

    private String mUserNo;//用户no
    private String mDriverAuthNo;//车辆no
    private CarriageInfoAdapter mCarriageInfoAdapter;
    private Map mMap = new HashMap();

    public static void start(Context context, String userNo, String driverAuthNo) {
        Intent intent = new Intent();
        intent.setClass(context, MoreCarriageInfoActivity.class);
        intent.putExtra("userNo", userNo);
        intent.putExtra("driverAuthNo", driverAuthNo);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder().appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        mUserNo = getIntent().getStringExtra("userNo");
        mDriverAuthNo = getIntent().getStringExtra("driverAuthNo");
        mMap.put("userNo", mUserNo);
        mMap.put("driverAuthNo", mDriverAuthNo);
        mModel.getCarriageListData(false, mMap);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_more_carriage_info);
        mBinding.include.titleView.setText("更多承运信息");
        mBinding.include.ivBack.setVisibility(View.VISIBLE);
        mBinding.include.ivBack.setOnClickListener(view -> finish());

        mCarriageInfoAdapter = new CarriageInfoAdapter(this, null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setAdapter(mCarriageInfoAdapter);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mModel.getCarriageListData(false, mMap);
            }

            @Override
            public void onLoadMore() {
                mModel.getCarriageListData(true, mMap);
            }
        });

        mCarriageInfoAdapter.setOnItemClick(new AdapterOnItemClick<CarrierDetailsEntity>() {
            @Override
            public void onItemClick(CarrierDetailsEntity carrierDetailsEntity, int position) {
                CarriageDetailActivity.start(MoreCarriageInfoActivity.this, carrierDetailsEntity.getCarrierNo(),1);
            }
        });
    }


    public void showList(List<CarrierDetailsEntity> list, boolean isLoadMore) {
        mBinding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                mBinding.recyclerView.loadMoreComplete();
                mCarriageInfoAdapter.addDatas(list);
            } else {
                mBinding.recyclerView.setNoMore(true);
            }
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            mBinding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                mBinding.emptyView.setVisibility(View.GONE);
                mCarriageInfoAdapter.setDatas(list);
                mBinding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                mCarriageInfoAdapter.setDatas(null);
                mBinding.recyclerView.setVisibility(View.GONE);
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}
